Completed
Push — master ( 94366e...dc9b8a )
by Maxence
04:37 queued 01:50
created

nav.initElementsCircleNavigation   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
/*
2
 * Circles - Bring cloud-users closer together.
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2017
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OC */
27
/** global: OCA */
28
/** global: Notyf */
29
30
/** global: actions */
31
/** global: nav */
32
/** global: elements */
33
/** global: settings */
34
/** global: resultCircles */
35
/** global: resultMembers */
36
/** global: resultGroups */
37
/** global: resultLinks */
38
/** global: curr */
39
/** global: api */
40
/** global: define */
41
42
var nav = {
43
44
	initNavigation: function () {
45
		this.initElementsAddMemberNavigation();
46
		this.initElementsLinkCircleNavigation();
47
		this.initElementsCircleNavigation();
48
49
		this.displayCirclesList('all');
50
	},
51
52
53
	initElementsAddMemberNavigation: function () {
54
55
		elements.addMember.hide();
56
		elements.addMember.on('input propertychange paste focus', function () {
57
			var search = $(this).val().trim();
58
			if (search === '') {
59
				elements.membersSearchResult.fadeOut(curr.animationMenuSpeed);
60
				return;
61
			}
62
63
			actions.searchMembersRequest(search);
64
			if (elements.membersSearchResult.children().length === 0) {
65
				elements.membersSearchResult.fadeOut(curr.animationMenuSpeed);
66
			} else {
67
				elements.membersSearchResult.fadeIn(curr.animationMenuSpeed);
68
			}
69
		}).blur(function () {
70
			setTimeout(function () {
71
				elements.membersSearchResult.fadeOut(curr.animationMenuSpeed);
72
				nav.circlesActionReturn();
73
			}, 100);
74
		});
75
		elements.addMember.on('keydown', function (e) {
76
			if (e.keyCode === 27) {
77
				nav.circlesActionReturn();
78
			}
79
			if (e.keyCode === 13) {
80
				api.addMember(curr.circle, $(this).val(), resultMembers.addMemberResult);
81
			}
82
		});
83
84
85
		elements.linkGroup.on('input propertychange paste focus', function () {
86
			var search = $(this).val().trim();
87
			if (search === '') {
88
				elements.groupsSearchResult.fadeOut(curr.animationMenuSpeed);
89
				return;
90
			}
91
92
			actions.searchGroupsRequest(search);
93
			if (elements.groupsSearchResult.children().length === 0) {
94
				elements.groupsSearchResult.fadeOut(curr.animationMenuSpeed);
95
			} else {
96
				elements.groupsSearchResult.fadeIn(curr.animationMenuSpeed);
97
			}
98
		}).blur(function () {
99
			setTimeout(function () {
100
				elements.groupsSearchResult.fadeOut(curr.animationMenuSpeed);
101
				nav.circlesActionReturn();
102
			}, 100);
103
		});
104
		elements.linkGroup.on('keydown', function (e) {
105
			if (e.keyCode === 27) {
106
				nav.circlesActionReturn();
107
			}
108
			if (e.keyCode === 13) {
109
				api.linkGroup(curr.circle, $(this).val(), resultGroups.linkGroupResult);
110
			}
111
		});
112
	},
113
114
115
	initElementsLinkCircleNavigation: function () {
116
117
		elements.linkCircle.hide();
118
		elements.linkCircle.on('keydown', function (e) {
119
120
			if (e.keyCode === 27) {
121
				nav.circlesActionReturn();
122
			}
123
			if (e.keyCode !== 13) {
124
				return;
125
			}
126
127
			api.linkCircle(curr.circle, elements.linkCircle.val(),
128
				resultLinks.linkCircleResult);
129
		}).blur(function () {
130
			nav.circlesActionReturn();
131
		});
132
	},
133
134
135
	initElementsCircleNavigation: function () {
136
137
		elements.joinCircle.hide();
138
		elements.joinCircle.on('click', function () {
139
			api.joinCircle(curr.circle, resultCircles.joinCircleResult);
140
			nav.circlesActionReturn();
141
		});
142
143
		elements.leaveCircle.hide();
144
		elements.leaveCircle.on('click', function () {
145
			OC.dialogs.confirm(
146
				t('circles', 'Are you sure you want to leave this circle?'),
147
				t('circles', 'Please confirm'),
148
				function (e) {
149
					if (e === true) {
150
						api.leaveCircle(curr.circle, resultCircles.leaveCircleResult);
151
						nav.circlesActionReturn();
152
					}
153
				});
154
		});
155
156
		elements.destroyCircle.on('click', function () {
157
			OC.dialogs.confirm(
158
				t('circles', 'Are you sure you want to delete this circle?'),
159
				t('circles', 'This action is irreversible'),
160
				function (e) {
161
					if (e === true) {
162
						api.destroyCircle(curr.circle, resultCircles.destroyCircleResult);
163
					}
164
				});
165
		});
166
167
		elements.joinCircleAccept.on('click', function () {
168
			api.joinCircle(curr.circle, resultCircles.joinCircleResult);
169
		});
170
171
		elements.joinCircleReject.on('click', function () {
172
			api.leaveCircle(curr.circle, resultCircles.leaveCircleResult);
173
		});
174
	},
175
176
177
	displayCirclesList: function (type) {
178
179
		curr.circlesType = type;
180
		curr.searchCircle = '';
181
		curr.searchUser = '';
182
183
		curr.circle = 0;
184
		curr.circleLevel = 0;
185
186
		elements.navigation.show('slide', 800);
187
		elements.emptyContent.show(800);
188
		elements.mainUI.fadeOut(800);
189
190
		elements.circlesSearch.val('');
191
		elements.addMember.val('');
192
		elements.linkCircle.val('');
193
194
		this.resetCirclesTypeSelection(type);
195
		elements.resetCirclesList();
196
		api.listCircles(type, '', 0, resultCircles.listCirclesResult);
197
	},
198
199
200
	resetCirclesTypeSelection: function (type) {
201
		elements.circlesList.children('div').removeClass('selected');
202
		elements.circlesList.children().each(function () {
203
			if ($(this).attr('circle-type') === type.toLowerCase()) {
204
				$(this).addClass('selected');
205
			}
206
		});
207
	},
208
209
	circlesActionReturn: function () {
210
		nav.displayCircleButtons(true);
211
		settings.displaySettings(false);
212
		nav.displayAddMemberInput(false);
213
		nav.displayLinkGroupInput(false);
214
		nav.displayLinkCircleInput(false);
215
		nav.displayJoinCircleButton(false);
216
		nav.displayInviteCircleButtons(false);
217
	},
218
219
	joinCircleAction: function () {
220
		nav.displayCircleButtons(false);
221
		nav.displayAddMemberInput(false);
222
		nav.displayLinkCircleInput(false);
223
		nav.displayLinkGroupInput(false);
224
		nav.displayJoinCircleButton(true);
225
	},
226
227
	displayCircleButtons: function (display) {
228
		if (display) {
229
			elements.buttonCircleActionReturn.hide(define.animationMenuSpeed);
230
			elements.buttonCircleActions.delay(define.animationMenuSpeed).show(
231
				define.animationMenuSpeed);
232
		} else {
233
			elements.buttonCircleActions.hide(define.animationMenuSpeed);
234
			elements.buttonCircleActionReturn.delay(define.animationMenuSpeed).show(
235
				define.animationMenuSpeed);
236
		}
237
	},
238
239
	displayAddMemberInput: function (display) {
240
		if (display) {
241
			elements.addMember.val('');
242
			elements.addMember.delay(define.animationMenuSpeed).show(define.animationMenuSpeed,
243
				function () {
244
					$(this).focus();
245
				});
246
		} else {
247
			elements.addMember.hide(define.animationMenuSpeed);
248
		}
249
	},
250
251
	displayLinkGroupInput: function (display) {
252
		if (display) {
253
			elements.linkGroup.val('');
254
			elements.linkGroup.delay(define.animationMenuSpeed).show(define.animationMenuSpeed,
255
				function () {
256
					$(this).focus();
257
				});
258
		} else {
259
			elements.linkGroup.hide(define.animationMenuSpeed);
260
		}
261
	},
262
263
	displayLinkCircleInput: function (display) {
264
		if (display) {
265
			elements.linkCircle.val('');
266
			elements.linkCircle.delay(define.animationMenuSpeed).show(define.animationMenuSpeed,
267
				function () {
268
					$(this).focus();
269
				});
270
		} else {
271
			elements.linkCircle.hide(define.animationMenuSpeed);
272
		}
273
	},
274
275
276
	displayInviteCircleButtons: function (display) {
277
		if (display) {
278
			elements.joinCircleAccept.show(define.animationMenuSpeed);
279
			elements.joinCircleReject.delay(define.animationMenuSpeed).show(
280
				define.animationMenuSpeed);
281
		} else {
282
			elements.joinCircleAccept.hide(define.animationMenuSpeed);
283
			elements.joinCircleReject.hide(define.animationMenuSpeed);
284
		}
285
	},
286
287
	displayJoinCircleButton: function (display) {
288
		if (display) {
289
			if (curr.circleStatus === 'Invited') {
290
				elements.joinCircle.hide(define.animationMenuSpeed);
291
				elements.leaveCircle.hide(define.animationMenuSpeed);
292
				nav.displayInviteCircleButtons(true);
293
294
			} else {
295
				nav.displayInviteCircleButtons(false);
296
297
				if (curr.circleLevel === 0 && curr.circleStatus !== 'Requesting') {
298
					elements.joinCircle.delay(define.animationMenuSpeed).show(
299
						define.animationMenuSpeed);
300
					elements.leaveCircle.hide(define.animationMenuSpeed);
301
					elements.joinCircleAccept.hide(define.animationMenuSpeed);
302
					elements.joinCircleReject.hide(define.animationMenuSpeed);
303
304
				}
305
				else {
306
					elements.leaveCircle.delay(define.animationMenuSpeed).show(
307
						define.animationMenuSpeed);
308
					elements.joinCircle.hide(define.animationMenuSpeed);
309
				}
310
			}
311
		} else {
312
			elements.joinCircle.hide(define.animationMenuSpeed);
313
			elements.leaveCircle.hide(define.animationMenuSpeed);
314
		}
315
	},
316
317
318
	/**
319
	 *
320
	 * @param display
321
	 */
322
	displayOptionsNewCircle: function (display) {
323
		if (display) {
324
			elements.newType.fadeIn(300);
325
			elements.newSubmit.fadeIn(500);
326
			elements.newTypeDefinition.fadeIn(700);
327
		}
328
		else {
329
			elements.newType.fadeOut(700);
330
			elements.newSubmit.fadeOut(500);
331
			elements.newTypeDefinition.fadeOut(300);
332
		}
333
	},
334
335
336
	displayMembers: function (members) {
337
		if (members === '') {
338
			members = curr.circleMembers;
339
		} else {
340
			curr.circleMembers = members;
341
		}
342
343
		elements.mainUIMembersTable.emptyTable();
344
		if (members === null) {
345
			elements.mainUIMembersTable.hide(200);
346
			return;
347
		}
348
349
		elements.mainUIMembersTable.show(200);
350
		for (var i = 0; i < members.length; i++) {
351
			var tmpl = elements.generateTmplMember(members[i]);
352
			elements.mainUIMembersTable.append(tmpl);
353
		}
354
355
		for (i = 0; i < 10; i++) {
356
			if (curr.circleLevel < 9 && curr.circleLevel <= i) {
357
				$('.level-select option[value="' + i + '"]').attr('disabled', 'disabled');
358
			}
359
		}
360
361
362
		elements.mainUIMembersTable.children('tr.entry').each(function () {
363
364
				var userId = $(this).attr('member-id');
365
366
				//
367
				// level
368
				var level = $(this).attr('member-level');
369
				var levelSelect = $(this).find('.level-select');
370
				if (level === '0') {
371
					levelSelect.hide();
372
				}
373
				else {
374
					levelSelect.show(200).val(level);
375
				}
376
				levelSelect.on('change', function () {
377
					actions.changeMemberLevel(userId, $(this).val());
378
				});
379
380
				//
381
				// status
382
				var status = $(this).attr('member-status');
383
				var statusSelect = $(this).find('.status-select');
384
385
				statusSelect.on('change', function () {
386
					actions.changeMemberStatus(userId, $(this).val());
387
				});
388
				statusSelect.append($('<option>', {
389
					value: status,
390
					text: t('circles', status)
391
				})).val(status);
392
393
				if (curr.circleLevel <= $(this).attr('member-level')) {
394
					return;
395
				}
396
397
				if (status === 'Member' || status === 'Invited') {
398
					statusSelect.append($('<option>', {
399
						value: 'remove_member',
400
						text: t('circles', 'Kick this member')
401
					}));
402
				}
403
404
				if (status === 'Requesting') {
405
					statusSelect.append($('<option>', {
406
						value: 'accept_request',
407
						text: t('circles', 'Accept the request')
408
					}));
409
					statusSelect.append($('<option>', {
410
						value: 'dismiss_request',
411
						text: t('circles', 'Dismiss the request')
412
					}));
413
				}
414
415
			}
416
		);
417
	},
418
419
420
	displayGroups: function (groups) {
421
		if (groups === '') {
422
			groups = curr.circleGroups;
423
		} else {
424
			curr.circleGroups = groups;
425
		}
426
427
		elements.mainUIGroupsTable.emptyTable();
428
		if (groups === null || groups.length === 0) {
429
			elements.mainUIGroupsTable.hide(curr.animationSpeed);
430
			return;
431
		}
432
433
		elements.mainUIGroupsTable.show(200);
434
		for (var i = 0; i < groups.length; i++) {
435
			var tmpl = elements.generateTmplGroup(groups[i]);
436
			elements.mainUIGroupsTable.append(tmpl);
437
		}
438
439
		for (i = 0; i < 10; i++) {
440
			if (curr.circleLevel < define.levelAdmin && curr.circleLevel <= i) {
441
				$('.level-select option[value="' + i + '"]').attr('disabled', 'disabled');
442
			}
443
		}
444
445
		elements.mainUIGroupsTable.children('tr.entry').each(function () {
446
447
				var groupId = $(this).attr('group-id');
448
449
				var level = $(this).attr('group-level');
450
				var levelSelect = $(this).find('.level-select');
451
				if (level === '0') {
452
					levelSelect.hide();
453
				}
454
				else {
455
					levelSelect.show(200).val(level);
456
				}
457
				levelSelect.append($('<option>', {
458
					value: 'remove_group',
459
					text: t('circles', 'Unlink this group')
460
				}));
461
462
				levelSelect.on('change', function () {
463
					actions.changeGroupLevel(groupId, $(this).val());
464
				});
465
			}
466
		);
467
	},
468
469
470
	displayLinks: function (links) {
471
472
		if (links === '') {
473
			links = curr.circleLinks;
474
		} else {
475
			curr.circleLinks = links;
476
		}
477
478
		elements.mainUILinksTable.hide(curr.animationSpeed);
479
		elements.mainUILinksTable.emptyTable();
480
		if (links === null || links.length === 0) {
481
			return;
482
		}
483
484
		elements.mainUILinksTable.show(curr.animationSpeed);
485
		for (var i = 0; i < links.length; i++) {
486
			var tmpl = elements.generateTmplLink(links[i]);
487
			elements.mainUILinksTable.append(tmpl);
488
		}
489
490
491
		elements.mainUILinksTable.children('tr.entry').each(function () {
492
493
			var linkId = $(this).attr('link-id');
494
			var status = parseInt($(this).attr('link-status'));
495
496
497
			var statusSelect = $(this).find('.link-status-select');
498
499
			statusSelect.on('change', function () {
500
				actions.changeLinkStatus(linkId, $(this).val());
501
			});
502
			statusSelect.append($('<option>', {
503
				value: status,
504
				text: define.linkStatus(status)
505
			})).val(status);
506
507
			if (curr.circleLevel < define.levelAdmin) {
508
				return;
509
			}
510
511
			if (status === define.linkSetup || status === define.linkRefused ||
512
				status === define.linkUp || status === define.linkDown) {
513
				statusSelect.append($('<option>', {
514
					value: define.linkRemove,
515
					text: t('circles', 'Remove this Link')
516
				}));
517
			}
518
519
			if (status === define.linkRequestSent) {
520
				statusSelect.append($('<option>', {
521
					value: define.linkRemove,
522
					text: t('circles', 'Cancel the Link request')
523
				}));
524
			}
525
526
			if (status === define.linkRequested) {
527
				statusSelect.append($('<option>', {
528
					value: define.linkUp,
529
					text: t('circles', 'Accept the Link request')
530
				}));
531
				statusSelect.append($('<option>', {
532
					value: define.linkRemove,
533
					text: t('circles', 'Reject the Link request')
534
				}));
535
			}
536
		});
537
	},
538
539
540
	displayCircleDetails: function (details) {
541
		elements.circleDetails.children('#name').text(details.name);
542
		elements.circleDesc.text(details.description);
543
544
		elements.circleDetails.children('#type').text(t('circles', details.typeLongString));
545
		if (details.description !== '') {
546
			elements.circleDesc.html(
547
				escapeHTML(details.description).replace(/\n/g, '&nbsp;<br />')).show(
548
				define.animationSpeed);
549
		}
550
		else {
551
			elements.circleDesc.text('').hide(define.animationSpeed);
552
		}
553
554
		elements.buttonCircleActions.show(300);
555
		elements.addMember.hide(300);
556
	},
557
558
559
	displayMembersInteraction: function (details) {
560
		if (details.user.level < define.levelModerator) {
561
			elements.buttonAddMember.hide();
562
		} else {
563
			elements.buttonAddMember.show();
564
		}
565
566
		nav.displayMemberInteractionCircleLinks(details);
567
		nav.displayMemberInteractionGroupLinks(details);
568
569
		elements.joinCircleInteraction.hide();
570
		elements.buttonJoinCircle.show();
571
572
		this.displayNonMemberInteraction(details);
573
574
		if (details.user.level === define.levelOwner) {
575
			elements.destroyCircle.show();
576
			elements.buttonCircleSettings.show();
577
			elements.buttonJoinCircle.hide();
578
		}
579
	},
580
581
582
	displayMemberInteractionGroupLinks: function (details) {
583
		if (curr.allowed_linked_groups === '0' ||
584
			details.user.level < define.levelAdmin
585
		) {
586
			elements.buttonLinkGroup.hide();
587
		}
588
		else {
589
			elements.buttonLinkGroup.show();
590
		}
591
	},
592
593
594
	displayMemberInteractionCircleLinks: function (details) {
595
		if (curr.allowed_federated_circles === '0' ||
596
			curr.circleSettings['allow_links'] !== 'true' ||
0 ignored issues
show
Coding Style introduced by
['allow_links'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
597
			details.type === 'Personal' ||
598
			details.user.level < define.levelAdmin
599
		) {
600
			elements.buttonLinkCircle.hide();
601
		}
602
		else {
603
			elements.buttonLinkCircle.show();
604
		}
605
	},
606
607
608
	displayNonMemberInteraction: function (details) {
609
		elements.joinCircleAccept.hide();
610
		elements.joinCircleReject.hide();
611
		elements.joinCircleRequest.hide();
612
		elements.joinCircleInvite.hide();
613
		elements.buttonCircleSettings.hide();
614
		elements.destroyCircle.hide();
615
616
		if (details.user.status === 'Requesting') {
617
			elements.joinCircleRequest.show();
618
			return;
619
		}
620
621
		if (details.user.status === 'Invited') {
622
			elements.joinCircleInvite.show();
623
			return;
624
		}
625
626
		if (details.user.level > 0) {
627
			return;
628
		}
629
630
		setTimeout(function () {
631
			nav.joinCircleAction();
632
		}, 200);
633
	}
634
635
};
636
637